﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SimpleTaskManager.Domain.Abstract;
using SimpleTaskManager.Domain.Entities;
using System.Web.Mvc;
using SimpleTaskManager.WebUI.Models;

namespace SimpleTaskManager.WebUI.Controllers
{
    /// <summary>
    /// A controller resposible to execute searches on the application's data.
    /// </summary>
    public class SearchController : Controller
    {
        private IProjectRepository projects;
        private ITaskRepository tasks;
        private IUserRepository users;
        private IReportRepository reports;

        /// <summary>
        /// Creates an instance of the SearchController.
        /// </summary>
        /// <param name="projects">A repository containing the projects.</param>
        /// <param name="tasks">A repository containing the tasks.</param>
        /// <param name="users">A repository containing the users.</param>
        /// <param name="reports">A repository containing the reports.</param>
        public SearchController(IProjectRepository projects, ITaskRepository tasks, IUserRepository users, IReportRepository reports)
        {
            this.projects = projects;
            this.tasks = tasks;
            this.users = users;
            this.reports = reports;
        }

        /// <summary>
        /// Renders the search input query form.
        /// </summary>
        /// <returns>The rendered partial view.</returns>
        public PartialViewResult SearchEngine()
        {
            return PartialView();
        }

        /// <summary>
        /// Displays search results for a given search string.
        /// </summary>
        /// <param name="search">The search query for which results will be displayed.</param>
        /// <returns>A view containing the search results.</returns>
        [Authorize]
        public ViewResult Index(string search = null)
        {
            SearchResultModel result = new SearchResultModel();
            ViewBag.Found = false;
            ViewBag.Message = "Sorry. No results found.";
            ViewBag.ShowEdit = User.IsInRole("SuperUser");

            if (search != null) 
            {
                search = search.ToLower();
                //search by users and their skills
                result.Users = users.Users.Where(u => u.FirstName.ToLower().Contains(search) ||
                            u.LastName.ToLower().Contains(search) || 
                            (u.User_Skills.Where(s => s.Skill.Name.ToLower().Contains(search)).Count() > 0 ) ).ToList();
                // search by projects
                result.Projects = projects.Projects.Where(p => p.MainTask.Name.ToLower().Contains(search) ||
                     p.MainTask.Leader.FirstName.ToLower().Contains(search) || p.MainTask.Leader.LastName.ToLower().Contains(search) ||
                     p.MainTask.ParentTask.Name.ToLower().Contains(search) ||
                     p.MainTask.Description.ToLower().Contains(search)).ToList();
                // search by tasks
                result.Tasks = tasks.Tasks.Where(t => t.Name.ToLower().Contains(search) ||
                        t.Leader.FirstName.ToLower().Contains(search) || t.Leader.LastName.ToLower().Contains(search) ||
                        t.ParentTask.Name.ToLower().Contains(search) ||
                        t.Description.ToLower().Contains(search)).ToList();
                //search by reports
                result.Reports = reports.ReportHeaders.Where(r => r.Name.ToLower().Contains(search) ||
                         r.Details.FileName.ToLower().Contains(search) || (r.CreatedBy.FirstName + " " + r.CreatedBy.LastName).ToLower().Contains(search) ||
                         r.Task.Name.ToLower().Contains(search) ).ToList();
                if (result.Users.Count > 0 || result.Tasks.Count > 0 || result.Projects.Count > 0 || result.Reports.Count > 0)
                {
                    ViewBag.Found = true;
                }
            }

            return View(result);
        }
    }
}